
--[[
	**********************
	* SvFrame - thomasfn *
	**********************
	health.lua - Loads basic health functionality
]]

function PLUGIN:InitHealth()
	svr.AddFlag( "health" )
	
	self:Command( "sethealth", self.SetHealth, { FLAG_HEALTH }  )
	self:Command( "addhealth", self.AddHealth, { FLAG_HEALTH }  )
end

table.insert( PLUGIN.Inits, PLUGIN.InitHealth )

function PLUGIN:SetHealth( ply, args )
	if (not svr.CheckArg( args, 1, "string")) then return ply:ChatPrint( colour.White, "Syntax: /sethealth <name> <amount>" ) end
	if (not svr.CheckArg( args, 2, "number")) then return ply:ChatPrint( colour.White, "Syntax: /sethealth <name> <amount>" ) end
	local plist = svr.FindPlayers( args[1] )
	if (#plist == 0) then return ply:ChatPrint( colour.Red, "No players found with that name." ) end
	if (#plist > 1) then return ply:ChatPrint( colour.Red, "Multiple players found with that partial name." ) end
	local amount = args[2] 
	local target = plist[1]
	if (target.Health() == tonumber(amount)) then return ply:ChatPrint( colour.Red, "Target health is already equal to ", amount, "!" ) end
	if (target.Health() > tonumber(amount)) then
		ply:ChatPrint( "Health of ", target:Name(), " set to ", colour.Red, amount, colour.White, "." )
		svr.ChatPrint( target, "You health has been set to " , colour.Red, amount, colour.White, " by ", ply:Name(), "." )
	else
		ply:ChatPrint( "Health of ", target:Name(), " set to ", colour.Green, amount, colour.White, "." )
		svr.ChatPrint( target, "You health has been set to " , colour.Green, amount, colour.White, " by ", ply:Name(), "." )
	end
	target.Health( tonumber(amount), true )
end

function PLUGIN:AddHealth( ply, args )
	if (not svr.CheckArg( args, 1, "string")) then return ply:ChatPrint( colour.White, "Syntax: /addhealth <name> <amount>" ) end
	if (not svr.CheckArg( args, 2, "number")) then return ply:ChatPrint( colour.White, "Syntax: /addhealth <name> <amount>" ) end
	local plist = svr.FindPlayers( args[1] )
	if (#plist == 0) then return ply:ChatPrint( colour.Red, "No players found with that name." ) end
	if (#plist > 1) then return ply:ChatPrint( colour.Red, "Multiple players found with that partial name." ) end
	local amount = args[2] 
	local target = plist[1]
	if (tonumber(amount) == 0) then return end
	if (tonumber(amount)> 0) then
		ply:ChatPrint( "Gave ", colour.Green, amount, colour.White, " health to ", target:Name(), "." )	
		svr.ChatPrint( target, ply:Name(), " has given ", colour.Green, amount, colour.White, " health to you.")
	else
		ply:ChatPrint( "Took ", colour.Red, tostring(-tonumber(amount)), colour.White, " health from ", target:Name(), "." )	
		svr.ChatPrint( target, ply:Name(), " has taken ", colour.Red, tostring(-tonumber(amount)), colour.White, " health from you.")
	end
	
	target.Health( tonumber(amount), false )
end